package jwdt.xadmin.info.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jwdt.common.bean.Tree;
import jwdt.common.dao.BaseDao;
import jwdt.xadmin.info.bean.TType;
import jwdt.xadmin.info.service.TypeManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.StringUtil;

public class TypeManageServiceImpl implements TypeManageService {
	
	private BaseDao<TType> baseDao;

	public void setBaseDao(BaseDao<TType> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil selectTypeList(PageUtil pu,int fid) throws Exception {
		String where = " fid=" + fid + " order by px";
		pu.doList("select * from t_type where "+where);
		return pu;
	}
	
	@Override
	public TType selectOneType(int typeid) throws Exception {
		return (TType)Jdb.get().get(TType.class, typeid);
	}
	
	@Override
	public String saveType(int typeid,TType obj) throws Exception {
		if(typeid==0){
			return baseDao.doAddObj(obj);
		}
		return baseDao.doUpdateObj(TType.class, typeid, obj);
	}
	
	@Override
	public String delType(int typeid) throws Exception {
		return baseDao.doDelObj(TType.class, typeid);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Tree> listTypeForTree(String jsdm)throws Exception {
		String sql = "select mxdm from jwdt_qx_jscdmx where cddm='xxfb' and jsdm='"+jsdm+"' and fldm='jscd'";
		List<Map> cds = Jdb.get().query(sql);
		List<TType> types = Jdb.get().query(TType.class,"bview=1 and fid=0 ORDER BY px");
		List<Integer> quanxian = new ArrayList<Integer>(); 
		for(Map map:cds){
			quanxian.add(StringUtil.parseInt((String) map.get("mxdm")));
		}
		return treeList(types,quanxian);
	}
	
	@SuppressWarnings("unchecked")
	public List<Tree> listTypeForTree()throws Exception{
		List<TType> types = Jdb.get().query(TType.class,"bview=1 and fid=0 ORDER BY px");
		List<Tree> list = new ArrayList<Tree>();
		Tree tree = new Tree();
		tree.setId("0");
		tree.setText("所有栏目");
		tree.setIconCls("icon-information");
		tree.setChildren(treeList(types,null));
		list.add(tree);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tree> treeList(List<TType> types,List<Integer> quanxian)throws Exception{
		List<Tree> treeList = new ArrayList<Tree>();
		for(TType type:types){
			Tree tree = new Tree();
			if(quanxian==null||quanxian.contains(type.getId())){
				tree.setId(type.getId()+"");
				tree.setText(type.getName());
				if(!type.getChildren().isEmpty()){
					tree.setState(Tree.STATE_CLOSED);//非叶子节点，否则死循环
					tree.setChildren(treeList(type.getChildren(),quanxian));
				}
				treeList.add(tree);
			}
		}
		return treeList;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tree> listAllTypeForTree()throws Exception{
		List<TType> types = Jdb.get().query(TType.class," fid=0 ORDER BY px");
		List<Tree> list = new ArrayList<Tree>();
		Tree tree = new Tree();
		tree.setId("0");
		tree.setText("所有栏目");
		tree.setIconCls("icon-information");
		tree.setChildren(treeList(types));
		list.add(tree);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tree> treeList(List<TType> types)throws Exception{
		List<Tree> treeList = new ArrayList<Tree>();
		for(TType type:types){
			Tree tree = new Tree();
			tree.setId(type.getId()+"");
			tree.setText(type.getName());
			if(!type.getAllChildren().isEmpty()){
				tree.setState(Tree.STATE_CLOSED);//非叶子节点，否则死循环
				tree.setChildren(treeList(type.getAllChildren()));
			}
			treeList.add(tree);
		}
		return treeList;
	}
}
